import router from "./router";
import pinia from "./stores";
import nprogress from "nprogress";
import "nprogress/nprogress.css";
import useSettingStore from "./stores/modules/setting";
import useUserStore from "./stores/modules/userStore";
const userStore = useUserStore(pinia);
let settingStore = useSettingStore(pinia);
router.beforeEach(async (to, from, next) => {
  nprogress.start();
  if (userStore.token) {
    if (to.path === "/login") {
      next("/");
    } else {
    
      //获取用户信息

      if (!settingStore.user) {
        await settingStore.getUserInfo();
        next({ ...to});
        
      } else {
        next();
      }
    }
  } else {
    if (to.path === "/login") {
      next();
    } else {
      next("/login");
    }
  }
});
router.afterEach((to,from) => {
  let tabObj = {
    fullpath: "",
    title: "",
  };
  tabObj.fullPath = to.fullPath;
  tabObj.title = to.meta?.title;
  settingStore.addTabBar(tabObj);
  
  nprogress.done();
});
